package com.test.controller;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.test.R;

/**
 * 对话框提示信息
 * AlertDialog
 * @Author Musique
 * 2020/8/7
 */
public class AlertDialogActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_dialog_one, btn_dialog_two, btn_dialog_tree, btn_dialog_four;

    private Context mContext;

    private AlertDialog alert = null;
    private AlertDialog.Builder builder = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alert_dialog_demo);
        mContext = this;
        bindViewShow();
    }


    /**
     * 处理常用查找id方法
     */
    public void bindViewShow() {
        btn_dialog_one = findViewById(R.id.btn_dialog_one);
        btn_dialog_two = findViewById(R.id.btn_dialog_two);
        btn_dialog_tree = findViewById(R.id.btn_dialog_tree);
        btn_dialog_four = findViewById(R.id.btn_dialog_four);

        // 创建监听
        btn_dialog_one.setOnClickListener(this);
        btn_dialog_two.setOnClickListener(this);
        btn_dialog_tree.setOnClickListener(this);
        btn_dialog_four.setOnClickListener(this);

    }

    /**
     * 点击事件
     */
    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            // 普通文本对话框
            case R.id.btn_dialog_one:
                alert = null;
                builder = new AlertDialog.Builder(mContext);
                alert = builder.setIcon(R.mipmap.ic_icon_fish)
                        .setTitle("系统提示：")
                        .setMessage("这是一个最普通的AlertDialog,\n带有三个按钮，分别是取消，中立和确定")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(mContext, "你点击了确定按钮~", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(mContext, "你点击了取消按钮~", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNeutralButton("中立", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(mContext, "你点击了中立按钮~", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .create(); //创建AlertDialog对象
                alert.show();     //显示对话框
                break;

            // 列表对话框
            case R.id.btn_dialog_two:
                //                List<String> list = new ArrayList<String>();
                //                list.add(0, "语文");
                //                list.add(1, "数学");
                //                list.add(2, "英语");
                //                list.add(3, "政治");
                //                list.add(4, "历史");
                //                list.add(5, "化学");
                //                list.add(6, "物理");
                //                list.add(7, "生物");
                //                list.add(8, "体育");
                final String str[] = {"语文", "数学", "英语", "历史", "政治", "化学", "物理", "生物", "体育"};
                alert = null;
                builder = new AlertDialog.Builder(mContext);
                alert = builder.setIcon(R.mipmap.iv_icon_5)
                        .setTitle("选择喜欢的课程：")
                        .setItems(str, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Toast.makeText(mContext, "你选择了：" + str[i], Toast.LENGTH_LONG).show();
                            }
                        })
                        .create();
                alert.show();
                break;

            // 单选
            case R.id.btn_dialog_tree:
                final String str1[] = {"语文", "数学", "英语", "历史", "政治", "化学", "物理", "生物", "体育"};
                alert = null;
                builder = new AlertDialog.Builder(mContext);
                alert = builder.setIcon(R.mipmap.iv_icon_3)
                        .setTitle("选择喜欢的课程：")
                        // 数字为默认选择
                        .setSingleChoiceItems(str1, 8, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Toast.makeText(mContext, "你选择了：" + str1[i], Toast.LENGTH_LONG).show();
                            }
                        })
                        .create();
                alert.show();
                break;
            // 多选
            case R.id.btn_dialog_four:
                final String str2[] = {"语文", "数学", "英语", "化学", "物理", "生物", "体育"};
                //定义一个用来记录个列表项状态的boolean数组
                final boolean checkItems[] = {false, false, false, false, false, false, false};
                alert = null;
                builder = new AlertDialog.Builder(mContext);
                alert = builder.setIcon(R.mipmap.iv_icon_4)
                        .setTitle("选择喜欢的课程：")
                        .setMultiChoiceItems(str2, checkItems, new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                                checkItems[i] = b;
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                String result = "";
                                for (int j = 0; j < checkItems.length; j++) {
                                    if (checkItems[j])
                                        result = result + str2[j] + " ";
                                }
                                Toast.makeText(getApplicationContext(), "喜欢的课程为:" + result, Toast.LENGTH_SHORT).show();
                            }
                        }).setCancelable(false)
                        .create();
                alert.show();
                break;
        }
    }
}
